package br.com.cept.sce.commons.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.cept.sce.dominio.IUsuario;

public class Servico {
	
	private static Servico instance;
	
	private Servico(){
		
	}
	
	public static Servico getInstance(){
		if(instance == null)
			instance = new Servico();
		return instance;
	}
	
	public void excluirObjetoSessao(String nome){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute(nome, null);
	}
	
	public Object recuperarObjetoSessao(String nome){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		return session.getAttribute(nome);
	}
	
	public void inserirObjetoSessao(String nome, Object objeto){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute(nome, objeto);
	}
	
	public void adicionarMensagemErro(String mensagem){
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(FacesMessage.SEVERITY_ERROR,mensagem, null));
	}
	
	public void adicionarMensagemAviso(String mensagem){
		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(FacesMessage.SEVERITY_INFO,mensagem, null));
	}
	
	public IUsuario obterUsuarioLogado(){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		return (IUsuario)session.getAttribute("usuario");
	}

}
